package com.norns.torque.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.hi.dhl.binding.viewbind
import com.norns.torque.databinding.DialogPayPwdBinding
import com.norns.torque.utils.SoftKeyboardUtils
import timber.log.Timber

class PwdDialog :CommonDialog(){
    companion object{
        fun newInstance():PwdDialog {
            val args = Bundle()
            val fragment = PwdDialog()
            fragment.arguments = args
            return fragment
        }
    }
    private val binding by viewbind<DialogPayPwdBinding>()
    private var btnClickListener:((isConfirm:Boolean)->Unit)?=null

    fun setClickListener(listener:(isConfirm:Boolean)->Unit){
        btnClickListener=listener
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        screenWidthAspect=0.9f
        return binding.root
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.btnConfirm.setOnClickListener {
            val content=binding.editPwd.inputContent
            Timber.d("content=${content}")
            if (content!="8888"){
                binding.editPwd.clearInputContent()
                Toast.makeText(requireActivity(), "密码错误", Toast.LENGTH_SHORT).show()
            }else{
                btnClickListener?.invoke(true)
                dismiss()
            }
        }
        binding.btnCancel.setOnClickListener {
            btnClickListener?.invoke(false)
            dismiss()
        }
    }

    override fun onResume() {
        super.onResume()
        val editText=binding.editPwd.getEditText()
        SoftKeyboardUtils.getInstance().showSoftKeyBoard(requireContext(), editText)
    }

    override fun hideKeyboard() {
        val editText=binding.editPwd.getEditText()
        SoftKeyboardUtils.getInstance().hideInput(requireContext(), editText)
        super.hideKeyboard()

    }
}